import { delDept, getDept, listDept, listDeptExcludeChild } from '@/api/system/dept'
import DictTag from '@/components/dict-tag/index.vue'
import { formatDate } from '@/utils/common'
import { useDicts } from '@/utils/dict'
import { message } from '@/utils/message'
import { handleTree } from '@pureadmin/utils'
import { ElMessageBox } from 'element-plus'
import { nextTick, onMounted, reactive, ref, toRefs } from 'vue'

export function useDept() {
  const data = reactive({
    form: {
      deptId: undefined,
      parentId: undefined,
      deptName: undefined,
      orderNum: 0,
      leader: undefined,
      phone: undefined,
      email: undefined,
      status: '0',
    },
    queryParams: {
      deptName: undefined,
      status: undefined,
    },
  })
  const { form, queryParams } = toRefs(data)
  const deptId = ref(0)
  const deptOptions = ref([])
  const title = ref('')
  const dataList = ref([])
  const loading = ref(true)
  const isExpandAll = ref(true)
  const refreshTable = ref(true)
  const formDialogVisible = ref(false)
  const { sys_normal_disable } = useDicts('sys_normal_disable')
  const columns: TableColumnList = [
    {
      label: '部门名称',
      prop: 'deptName',
      align: 'left',
      minWidth: 150,
    },
    {
      label: '显示排序',
      prop: 'orderNum',
      minWidth: 80,
    },
    {
      label: '负责人',
      prop: 'leader',
      minWidth: 150,
    },
    {
      label: '联系电话',
      prop: 'phone',
      minWidth: 150,
    },
    {
      label: '邮箱',
      prop: 'email',
      minWidth: 150,
    },
    {
      label: '状态',
      prop: 'status',
      minWidth: 130,
      cellRenderer: (scope) => {
        return <DictTag options={sys_normal_disable} value={scope.row.status} />
      },
    },
    {
      label: '创建时间',
      minWidth: 180,
      prop: 'createTime',
      formatter: ({ createTime }) => formatDate(createTime),
    },
    {
      label: '操作',
      fixed: 'right',
      width: 180,
      slot: 'operation',
    },
  ]

  /** 重置新增的表单  */
  function reset() {
    form.value = {
      deptId: undefined,
      parentId: undefined,
      deptName: undefined,
      orderNum: 0,
      leader: undefined,
      phone: undefined,
      email: undefined,
      status: '0',
    }
  }

  /** 新增按钮操作 */
  function handleAdd(row) {
    reset()
    listDept().then((response: any) => {
      deptOptions.value = handleTree(response.data, 'deptId')
    })
    if (row != undefined) {
      form.value.parentId = row.deptId
    }
    deptId.value = row.deptId
    formDialogVisible.value = true
  }

  /** 展开/折叠操作 */
  function toggleExpandAll() {
    refreshTable.value = false
    isExpandAll.value = !isExpandAll.value
    nextTick(() => {
      refreshTable.value = true
    })
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset()
    listDeptExcludeChild(row.deptId).then((response: any) => {
      deptOptions.value = handleTree(response.data, 'deptId')
    })
    deptId.value = row.deptId
    formDialogVisible.value = true
    getDept(deptId.value).then((response: any) => {
      form.value = response.data
      formDialogVisible.value = true
      title.value = '修改部门'
    })
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    ElMessageBox.confirm('数据一经删除不可恢复，请再次确认！', '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async () => {
      await delDept(row.deptId)
      message('删除成功', { type: 'success' })
      onSearch()
    })
  }

  /** 搜索按钮操作 */
  async function onSearch() {
    loading.value = true
    const { data } = await listDept(queryParams.value)
    const menuList = handleTree(data, 'deptId')
    dataList.value = menuList
    loading.value = false
  }

  /** 重置按钮操作 */
  const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
    onSearch()
  }

  onMounted(() => {
    onSearch()
  })

  return {
    form,
    title,
    deptId,
    loading,
    columns,
    dataList,
    isExpandAll,
    deptOptions,
    queryParams,
    refreshTable,
    formDialogVisible,
    sys_normal_disable,
    onSearch,
    resetForm,
    handleAdd,
    handleUpdate,
    handleDelete,
    toggleExpandAll,
  }
}
